public class Solution564 {
    public String nearestPalindromic(String n) {
        int pos=n.length()/2;
        String n1;
        String n2;
        String n3;
        if (n.length()%2==0){
            String s=n.substring(0,pos);
            Long l=Long.parseLong(s);
            String sMax=String.valueOf(l+1);
            String sMin=String.valueOf(l-1);
            n1=s.concat(new StringBuilder(s).reverse().toString());
            if (sMin.length()==s.length() && !sMin.equals("0")) {
                n2=sMin.concat(new StringBuilder(sMin).reverse().toString());
            }else{
                StringBuilder stringBuilder=new StringBuilder();
                for (int i=0;i<n.length()-1;i++){
                    stringBuilder.append(9);
                }
                n2=stringBuilder.toString();
            }
            if (sMax.length()==s.length()){
                n3=sMax.concat(new StringBuilder(sMax).reverse().toString());
            }else{
                n3=sMax.concat(new StringBuilder(sMax.substring(0,sMax.length()-1)).reverse().toString());
            }
        }else{
            String s=n.substring(0,pos+1);
            Long l=Long.parseLong(s);
            String sMax=String.valueOf(l+1);
            String sMin=String.valueOf(l-1);
            n1=s.concat(new StringBuilder(s.substring(0,s.length()-1)).reverse().toString());
            if (sMin.length()==s.length()) {
                n2=sMin.concat(new StringBuilder(sMin.substring(0, sMin.length() - 1)).reverse().toString());
            }else{
                n2=sMin.concat(new StringBuilder(sMin).reverse().toString());
            }
            if (sMax.length()==s.length()) {
                n3=sMax.concat(new StringBuilder(sMax.substring(0, sMax.length() - 1)).reverse().toString());
            }else{
                StringBuilder stringBuilder=new StringBuilder();
                stringBuilder.append(1);
                for (int i=0;i<n.length()-1;i++){
                    stringBuilder.append(0);
                }
                stringBuilder.append(1);
                n3=stringBuilder.toString();
            }
        }
        Long target=Long.parseLong(n);
        Long max=Long.parseLong(n3);
        Long min=Long.parseLong(n2);
        Long nor=Long.parseLong(n1);
        Long sub=Math.abs(nor-target);
        String t=String.valueOf(nor);
        if (sub==0){
            sub=Long.MAX_VALUE;
        }
        if (target-min<=sub){
            sub=target-min;
            t=String.valueOf(min);
        }
        if (max-target<sub){
            t=String.valueOf(max);
        }
        return t;
    }

    public static void main(String[] args) {
        System.out.println(new Solution564().nearestPalindromic("10"));
    }
}
